<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-socialite" tabindex="-1"><a class="header-anchor" href="#laravel-socialite"><span>Laravel Socialite</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#installation">安装</a></li>
<li><a href="#upgrading-socialite">升级</a></li>
<li><a href="#configuration">配置</a></li>
<li><a href="#authentication">身份验证</a>
<ul>
<li><a href="#routing">路由</a></li>
<li><a href="#authentication-and-storage">身份验证和存储</a></li>
<li><a href="#access-scopes">访问范围</a></li>
<li><a href="#slack-bot-scopes">机器人访问范围</a></li>
<li><a href="#optional-parameters">可选参数</a></li>
</ul>
</li>
<li><a href="#retrieving-user-details">获取用户详细信息</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>除了典型的基于表单的身份验证外，Laravel 还提供了一种简单、方便的方式，使用 <a href="https://github.com/laravel/socialite" target="_blank" rel="noopener noreferrer">Laravel Socialite</a> 与 OAuth 提供者进行身份验证。Socialite 目前支持通过 Facebook、Twitter、LinkedIn、Google、GitHub、GitLab、Bitbucket 和 Slack 进行身份验证。</p>
<blockquote>
<p>[!NOTE]<br>
社区驱动的 <a href="https://socialiteproviders.com/" target="_blank" rel="noopener noreferrer">Socialite Providers</a> 网站提供了其他平台的适配器。</p>
</blockquote>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>要开始使用 Socialite，请使用 Composer 包管理器将包添加到项目的依赖项中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/socialite</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="升级" tabindex="-1"><a class="header-anchor" href="#升级"><span>升级</span></a></h2>
<p>升级到 Socialite 的新主要版本时，重要的是仔细查看<a href="https://github.com/laravel/socialite/blob/master/UPGRADE.md" target="_blank" rel="noopener noreferrer">升级指南</a>。</p>
<h2 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h2>
<p>在使用 Socialite 之前，您需要为您的应用程序使用的 OAuth 提供者添加凭据。通常，这些凭据可以通过在您将要进行身份验证的服务的仪表板中创建一个“开发人员应用程序”来检索。</p>
<p>这些凭据应放置在您的应用程序的 <code v-pre>config/services.php</code> 配置文件中，并且应使用键 <code v-pre>facebook</code>、<code v-pre>twitter</code>（OAuth 1.0）、<code v-pre>twitter-oauth-2</code>（OAuth 2.0）、<code v-pre>linkedin-openid</code>、<code v-pre>google</code>、<code v-pre>github</code>、<code v-pre>gitlab</code>、<code v-pre>bitbucket</code>、<code v-pre>slack</code> 或 <code v-pre>slack-openid</code>，具体取决于您的应用程序所需的提供者：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'github'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'client_id'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'GITHUB_CLIENT_ID'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'client_secret'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'GITHUB_CLIENT_SECRET'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'redirect'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/callback-url'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!NOTE]<br>
如果 <code v-pre>redirect</code> 选项包含相对路径，它将自动解析为完全合格的 URL。</p>
</blockquote>
<h2 id="身份验证" tabindex="-1"><a class="header-anchor" href="#身份验证"><span>身份验证</span></a></h2>
<h3 id="路由" tabindex="-1"><a class="header-anchor" href="#路由"><span>路由</span></a></h3>
<p>要使用 OAuth 提供者对用户进行身份验证，你需要两个路由：一个用于将用户重定向到 OAuth 提供者，另一个用于在身份验证后接收提供者的回调。下面的示例路由演示了这两个路由的实现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/auth/redirect'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/auth/callback'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// $user->token</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>Socialite</code> 门面提供的 <code v-pre>redirect</code> 方法负责将用户重定向到 OAuth 提供者，而 <code v-pre>user</code> 方法将检查传入的请求并在用户批准身份验证请求后从提供者那里检索用户信息。</p>
<h3 id="身份验证和存储" tabindex="-1"><a class="header-anchor" href="#身份验证和存储"><span>身份验证和存储</span></a></h3>
<p>一旦从 OAuth 提供者检索到用户，你可以确定用户是否存在于您的应用程序数据库中，并<a href="https://learnku.com/docs/laravel/11.x/authentication#authenticate-a-user-instance" target="_blank" rel="noopener noreferrer">对用户进行身份验证</a>。如果用户不存在于您的应用程序数据库中，通常会在数据库中创建一个新记录来表示该用户：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Auth</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/auth/callback'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$githubUser</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">updateOrCreate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'github_id'</span> <span class="token operator">=></span> <span class="token variable">$githubUser</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token variable">$githubUser</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token variable">$githubUser</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'github_token'</span> <span class="token operator">=></span> <span class="token variable">$githubUser</span><span class="token operator">-></span><span class="token property">token</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'github_refresh_token'</span> <span class="token operator">=></span> <span class="token variable">$githubUser</span><span class="token operator">-></span><span class="token property">refreshToken</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">login</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!NOTE]<br>
有关从特定 OAuth 提供者检索用户信息的更多信息，请参阅<a href="#retrieving-user-details">获取用户详细信息</a>上的文档。</p>
</blockquote>
<h3 id="访问范围" tabindex="-1"><a class="header-anchor" href="#访问范围"><span>访问范围</span></a></h3>
<p>在重定向用户之前，你可以使用 <code v-pre>scopes</code> 方法来指定应包含在身份验证请求中的「范围」。此方法将会将之前指定的所有范围与你指定的范围合并：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">scopes</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'read:user'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public_repo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>setScopes</code> 方法重写身份验证请求中的所有现有范围：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setScopes</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'read:user'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public_repo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="slack-机器人范围" tabindex="-1"><a class="header-anchor" href="#slack-机器人范围"><span>Slack 机器人范围</span></a></h3>
<p>Slack 的 API 提供了<a href="https://api.slack.com/authentication/token-types" target="_blank" rel="noopener noreferrer">不同类型的访问令牌</a>，每种类型都有其自己的一组<a href="https://api.slack.com/scopes" target="_blank" rel="noopener noreferrer">权限范围</a>。Socialite 兼容以下两种 Slack 访问令牌类型：</p>
<ul>
<li>机器人（以 <code v-pre>xoxb-</code> 为前缀）</li>
<li>用户（以 <code v-pre>xoxp-</code> 为前缀）</li>
</ul>
<p>默认情况下，<code v-pre>slack</code> 驱动程序将生成一个 <code v-pre>user</code> 令牌，并调用驱动程序的 <code v-pre>user</code> 方法将返回用户的详细信息。</p>
<p>如果你的应用程序将向由你的应用程序用户拥有的外部 Slack 工作区发送通知，则机器人令牌非常有用。要生成机器人令牌，请在将用户重定向到 Slack 进行身份验证之前调用 <code v-pre>asBotUser</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'slack'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">asBotUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setScopes</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'chat:write'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'chat:write.public'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'chat:write.customize'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，在 Slack 将用户重定向回你的应用程序进行身份验证后，必须在调用 <code v-pre>user</code> 方法之前调用 <code v-pre>asBotUser</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'slack'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">asBotUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在生成机器人令牌时，<code v-pre>user</code> 方法仍将返回一个 <code v-pre>Laravel\Socialite\Two\User</code> 实例；但是，只会填充 <code v-pre>token</code> 属性。可以存储此令牌，以便<a href="https://learnku.com/docs/laravel/11.x/notifications#notifying-external-slack-workspaces" target="_blank" rel="noopener noreferrer">向经过身份验证的用户的 Slack 工作区发送通知</a>。</p>
<h3 id="可选参数" tabindex="-1"><a class="header-anchor" href="#可选参数"><span>可选参数</span></a></h3>
<p>许多 OAuth 提供程序支持在重定向请求中使用其他可选参数。要在请求中包含任何可选参数，请使用带有关联数组的 <code v-pre>with</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'google'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'hd'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'example.com'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!WARNING]<br>
使用 <code v-pre>with</code> 方法时，请注意不要传递任何保留关键字，如 <code v-pre>state</code> 或 <code v-pre>response_type</code>。</p>
</blockquote>
<h2 id="获取用户详细信息" tabindex="-1"><a class="header-anchor" href="#获取用户详细信息"><span>获取用户详细信息</span></a></h2>
<p>在用户被重定向回你的应用程序的身份验证回调路由后，你可以使用 Socialite 的 <code v-pre>user</code> 方法来检索用户的详细信息。<code v-pre>user</code> 方法返回的用户对象提供了各种属性和方法，你可以使用它们来存储关于用户的信息到你自己的数据库中。</p>
<p>根据你正在进行身份验证的 OAuth 提供程序是否支持 OAuth 1.0 或 OAuth 2.0，此对象上可能可用不同的属性和方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/auth/callback'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// OAuth 2.0 提供程序...</span></span>
<span class="line">    <span class="token variable">$token</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">token</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$refreshToken</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">refreshToken</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$expiresIn</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">expiresIn</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// OAuth 1.0 提供程序...</span></span>
<span class="line">    <span class="token variable">$token</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">token</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$tokenSecret</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">tokenSecret</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 所有提供程序...</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getNickname</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getEmail</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getAvatar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你已经拥有用户的有效访问令牌，你可以使用 Socialite 的 <code v-pre>userFromToken</code> 方法检索他们的用户详细信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">userFromToken</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你正在使用 iOS 应用程序通过 Facebook 有限登录，Facebook 将返回一个 OIDC 令牌而不是访问令牌。类似于访问令牌，OIDC 令牌可以提供给 <code v-pre>userFromToken</code> 方法以检索用户详细信息。</p>
<h4 id="从令牌和密钥检索用户详细信息-oauth1" tabindex="-1"><a class="header-anchor" href="#从令牌和密钥检索用户详细信息-oauth1"><span>从令牌和密钥检索用户详细信息（OAuth1）</span></a></h4>
<p>如果你已经拥有用户的有效令牌和密钥，你可以使用 Socialite 的 <code v-pre>userFromTokenAndSecret</code> 方法检索他们的用户详细信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'twitter'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">userFromTokenAndSecret</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">,</span> <span class="token variable">$secret</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="无状态身份验证" tabindex="-1"><a class="header-anchor" href="#无状态身份验证"><span>无状态身份验证</span></a></h4>
<p><code v-pre>stateless</code> 方法可用于禁用会话状态验证。当向不使用基于 cookie 的会话的无状态 API 添加社交身份验证时，这很有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'google'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">stateless</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!WARNING]<br>
无状态身份验证不适用于 Twitter OAuth 1.0 驱动程序。</p>
</blockquote>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/socialitemd/16734" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/so...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/socialitemd/16734" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/so...</a></p>
</blockquote>
</div></template>


